<?php
//include("../../db/DBConnection.php");
//DataBase::createConection();
include("../../db/cart_db.php");
include("../../db/cart_functions.php");

if (isset($_REQUEST['command']) && $_REQUEST['command'] == 'update') {
    $name = $_REQUEST['name'];
    $email = $_REQUEST['email'];
    $address = $_REQUEST['address'];
    $phone = $_REQUEST['phone'];

    $result = mysql_query("insert into clientes values('','$name','$email','$address','$phone')");
    $customerid = mysql_insert_id();
    $date = date('Y-m-d H:i:s');
    $result = mysql_query("insert into pedidos values('','$date','$customerid')");
    $orderid = mysql_insert_id();

    $max = count($_SESSION['cart']);
    for ($i = 0; $i < $max; $i++) {
        $pid = $_SESSION['cart'][$i]['productid'];
        $q = $_SESSION['cart'][$i]['qty'];
        $price = get_price($pid);
        mysql_query("INSERT INTO `detalhes_pedido`(`id_pedido`, `id_produto`, `quantidade`, `preco`) VALUES ($orderid,$pid,$q,$price)");
    }
    die('Obrigado por comprar em nossa loja! Seu pedido foi realizado!');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Bfat</title>
        <!-- <link rel="shortcut icon" href="../imagens/favicon.ico" /> -->
        <link href="../../viewer/css/styles.css" rel="stylesheet" type="text/css" />
        <link href="../../viewer/css/button.css" rel="stylesheet" type="text/css" />
        <script language="javascript">
            function validate() {
                var f = document.form1;
                if (f.name.value == '') {
                    alert('É necessário entrar com seu nome!');
                    f.name.focus();
                    return false;
                }
                f.command.value = 'update';
                f.submit();
            }
        </script>
        <style>
            div#side-col
            {
                top: 400px;
            }
            td{
                padding:15px;
                background-color:#FFFFFF;

            }
        </style>
    </head>

    <body>
        <div id="banner">
            <center><a href="../../viewer/vIndex.php"><img width="200" height="200" src="../../viewer/images/logo.png" /></a>
                <nav><ul>
                        <li><a href="../../viewer/shopping_cart/products.php">Cardápio</a></li>
                        <li><a href="../../viewer/vLocalizacao.php">Localização</a></li>
                        <li><a href="../../viewer/vFotos.php">Fotos</a></li>
                        <li><a href="../../viewer/vContato.php">Contato</a></li>
                        <li><a href="../../viewer/vLogin.php">Painel de controle</a></li>
                    </ul></nav></center>
        </div>

        <div id="side-col">
            <form name="form1" onsubmit="return validate()">
                <input type="hidden" name="command" />
                <div align="center">
                    <h3 align="center">Informações de cobrança</h3>
                    <table border="0" cellpadding="2px">
                        <tr><td>Total: R$</td><td><?php echo get_order_total() ?></td></tr>
                        <tr><td>Seu nome:</td><td><input type="text" name="name" required/></td></tr>
                        <tr><td>Endereço:</td><td><input type="text" name="address" required/></td></tr>
                        <tr><td>Email:</td><td><input type="text" name="email" required placeholder="seu@email.com"/></td></tr>
                        <tr><td>Telefone:</td><td><input type="text" name="phone" required pattern="\([0-9]{2}\)[\s][0-9]{4}-[0-9]{4}" placeholder="(xx) xxxx-xxxx"/></td></tr>
                        <tr><td>&nbsp;</td><td><input type="submit" value="Fechar pedido" /></td></tr>
                    </table>
                </div>
        </div>
        </form>
    </body>
</html>